﻿// ------------------------------------------------------------------------------------
//      Copyright (c) uhavemyword@gmail.com. All rights reserved.
//      Created by Ben at 02/19/2013 21:51:53
// ------------------------------------------------------------------------------------

namespace CP.NLayer.Client.WpfClient.Common
{
    using System;
    using CP.NLayer.Resources.UI;
    using Telerik.Windows.Controls;

    public class InteractionService : IInteractionService
    {
        private Action _onConfirmed;
        private Action _onAlertClosed;
        private Action _onErrorClosed;

        public void ShowDeleteConfirmation(string content, Action onConfirmed)
        {
            this._onConfirmed = onConfirmed;
            RadWindow.Confirm(new DialogParameters()
            {
                Content = content,
                Closed = new EventHandler<WindowClosedEventArgs>(OnDeleteConfirm),
                DialogStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
                Header = UiResources.Confirm,
                OkButtonContent = UiResources.Ok,
                CancelButtonContent = UiResources.Cancel
            });
        }

        public void ShowEditView(Type editViewType, string id, Action onEdited)
        {
            if (editViewType == null)
            {
                this.ShowAlert("editViewType should not be null!", null);
                return;
            }

            RadWindow view;
            try
            {
                view = (RadWindow)Activator.CreateInstance(editViewType, id);
            }
            catch (Exception ex)
            {
                string msg = "Couldn't create EditView (RadWindow type). " + Environment.NewLine + ex.Message;
                this.ShowAlert(msg, null);
                return;
            }

            view.ShowDialog();
            if (view.DialogResult == true && onEdited != null)
            {
                onEdited();
            }
        }

        public void ShowAlert(string content, Action onAlertClosed)
        {
            this._onAlertClosed = onAlertClosed;
            RadWindow.Alert(new DialogParameters()
            {
                Content = content,
                Closed = new EventHandler<WindowClosedEventArgs>(OnAlertClosed),
                DialogStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
                Header = UiResources.Alert,
                OkButtonContent = UiResources.Ok
            });
        }

        public void ShowError(string content, Action onErrorClosed)
        {
            this._onErrorClosed = onErrorClosed;
            RadWindow.Alert(new DialogParameters()
            {
                Content = content,
                Closed = new EventHandler<WindowClosedEventArgs>(OnErrorClosed),
                DialogStartupLocation = System.Windows.WindowStartupLocation.CenterScreen,
                Header = UiResources.Error,
                OkButtonContent = UiResources.Ok
            });
        }

        private void OnDeleteConfirm(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true && this._onConfirmed != null)
            {
                this._onConfirmed();
            }
        }

        private void OnAlertClosed(object sender, WindowClosedEventArgs e)
        {
            if (this._onAlertClosed != null)
            {
                this._onAlertClosed();
            }
        }

        private void OnErrorClosed(object sender, WindowClosedEventArgs e)
        {
            if (this._onErrorClosed != null)
            {
                this._onErrorClosed();
            }
        }
    }
}
